/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  getWxList,
  checkScan,
  getAuthQrcode,
  getWxCourierList,
  importCourier
} from "@/services/operator_wx";

export default {
  namespace: "operator_wx",

  state: {
    wxCourierData: null,
    qrcodeData: null,
    wxList: null
  },

  effects: {
    // checkScan,
    * getWxCourierList({ payload, then }, { call, put }) {
      const response = yield call(getWxCourierList, payload);
      if (!response) return;
      const { msg, code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "updateState",
          payload: {
            wxCourierData: data
          }
        });
      } else {
        message.error(msg);
      }
      then && then(response);
    },
    * getWxList({ payload, then }, { call, put }) {
      const response = yield call(getWxList, payload);
      if (!response) return;
      const { msg, code, data } = response;
      const { list } = data || {};
      // if (code > 0 || !list) {
      //   message.error("请先绑定公众号");
      // }
      yield put({
        type: "updateState",
        payload: {
          wxList: list
        }
      });
      then && then(response);
    },
    * getAuthQrcode({ payload, then }, { call, put }) {
      const response = yield call(getAuthQrcode, payload);
      if (!response) return;
      const { msg, code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "updateState",
          payload: {
            qrcodeData: data
          }
        });
      } else {
        message.error(msg);
      }
      then && then(response);
    },
    * importCourier({ payload, then }, { call }) {
      const response = yield call(importCourier, payload);
      if (!response) return;
      const { msg, code } = response;
      if (code > 0) {
        message.error(msg);
      } else {
        message.success("导入成功");
      }
      then && then(response);
    }
  },

  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};